<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24/024
 * Time: 20:12
 */

namespace app\admin\validate;

class TaskValidate extends \think\Validate
{

    const SCENE_ADD = 'add'; //添加
    const SCENE_EDIT = 'edit'; //编辑
    const SCENE_REMOVE = 'remove'; //删除数据
    const SCENE_CHECK = 'check'; //审核
    const SCENE_SET_NUM = 'set_num'; //设置数量
    const SCENE_SET_AWARD = 'set_award'; //设置金额
    const SCENE_UNDER = 'under'; //下架




    protected $rule = [
        'id' => 'require|number',
        'type_id' => 'require|number',
        'type' => 'require|number',
        'title' => 'require|max:50',
        'total_num'  =>  'require|integer|>:0',
        'award' => 'require|gt:0|_isNumber',
        'num' => 'require|gt:0',
    ];


    protected $message  = [
        'type_id.require' => '任务类型必须',
        'type_id.number' => '任务类型必须为数字',
        'type.require' => '操作类型必须',
        'type.number' => '操作类型必须为数字',
        'title.require' => '标题必须',
        'title.max' => '标题最大长度50',
        'total_num.require' => '数量必须',
        'total_num.integer' => '数量必须为整数',
        'total_num.gt' => '数量必须大于0',
        'award.require' => '奖励金额必填',
        'award.gt' => '奖励金币必须大于0',
        'num.require' => '任务数量必填',
        'num.gt' => '任务数量必须大于0',

    ];
    
    //场景
    protected $scene  = [
       self::SCENE_ADD => ['type_id','title', 'total_num', 'award'],
       self::SCENE_EDIT => ['id','type_id','title', 'total_num', 'award'],
       self::SCENE_REMOVE => ['id'],
       self::SCENE_CHECK => ['id'],
       self::SCENE_UNDER => ['id'],
       self::SCENE_SET_NUM => ['id', 'type', 'num'],
       self::SCENE_SET_AWARD => ['id', 'award'],
    ];
    
    
    /**
    * 验证奖励为小数或整数
    */
    public function _isNumber($val){
        $reg = "/^(([0-9]+|0)\.([0-9]{1,2})$)|^([0-9]+|0)$/";
        return preg_match($reg, $val) ? true : '请输入合法的数字';
    }
    

}